package com.ming;

import com.baomidou.mybatisplus.core.exceptions.MybatisPlusException;
import com.baomidou.mybatisplus.core.toolkit.StringPool;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;

public class CodeGenerator {

    /**
     * <p>
     * 读取控制台内容
     * </p>
     */
    public static String scanner(String tip) {
        Scanner scanner = new Scanner(System.in);
        StringBuilder help = new StringBuilder();
        help.append("请输入" + tip + "：");
        System.out.println(help.toString());
        if (scanner.hasNext()) {
            String ipt = scanner.next();
            if (!StringUtils.isEmpty(ipt)) {
                return ipt;
            }
        }
        throw new MybatisPlusException("请输入正确的" + tip + "！");
    }

    public static void main(String[] args) {
        // 代码生成器
        AutoGenerator mpg = new AutoGenerator();

        // 全局配置
        GlobalConfig gc = new GlobalConfig();
        String projectPath = System.getProperty("user.dir");
        gc.setOutputDir("e://ljmcode");
        gc.setAuthor("ming");
        gc.setOpen(true);
        gc.setSwagger2(true);
        gc.setFileOverride(true);
        mpg.setGlobalConfig(gc);
        // 切换为 freemarker 模板引擎
        mpg.setTemplateEngine(new FreemarkerTemplateEngine());

        // 数据源配置
        DataSourceConfig dsc = new DataSourceConfig();
        dsc.setUrl("jdbc:mysql://47.107.129.179:3306/ljm_sys_base?useUnicode=true&characterEncoding=UTF-8&useSSL=false&serverTimezone=UTC");
        // dsc.setSchemaName("public");
        dsc.setDriverName("com.mysql.cj.jdbc.Driver");
        dsc.setUsername("ljm_sys_base");
        dsc.setPassword("DiZDWAANk54ecM5k");
        mpg.setDataSource(dsc);

        // 包配置
        PackageConfig pc = new PackageConfig();
        pc.setParent("com.ljm.sys");
        pc.setModuleName("org");
        mpg.setPackageInfo(pc);

        // 自定义配置
        InjectionConfig cfg = new InjectionConfig() {
            @Override
            public void initMap() {
                Map<String, Object> map = new HashMap<String, Object>();
                map.put("dtoPackage", pc.getParent() + ".dto");
                map.put("apiPackage", pc.getParent() + ".api");
                this.setMap(map);
            }
        };
        ArrayList<FileOutConfig> focList = new ArrayList<>();
        //dto模版配置
        focList.add(new FileOutConfig("/templates/generator/dto.java.ftl") {
            @Override
            public String outputFile(TableInfo tableInfo) {
                // 自定义输入文件名称
                return gc.getOutputDir() + "//"
                        + pc.getParent().replace(".", "//")
                        + "//" + "dto" + "//"
                        + tableInfo.getEntityName() + "Dto" + StringPool.DOT_JAVA;
            }
        });
        //api模版配置
        focList.add(new FileOutConfig("/templates/generator/api.java.ftl") {
            @Override
            public String outputFile(TableInfo tableInfo) {
                // 自定义输入文件名称
                return gc.getOutputDir() + "//"
                        + pc.getParent().replace(".", "//")
                        + "//" + "api" + "//"
                        + tableInfo.getEntityName() + "Api" + StringPool.DOT_JAVA;
            }
        });
        cfg.setFileOutConfigList(focList);
        mpg.setCfg(cfg);

        TemplateConfig templateConfig = new TemplateConfig();
        templateConfig.setController("/templates/generator/controller.java");
        templateConfig.setServiceImpl("/templates/generator/serviceImpl.java");

        mpg.setTemplate(templateConfig);

        // 策略配置isDelete
        StrategyConfig strategy = new StrategyConfig();
        strategy.setNaming(NamingStrategy.underline_to_camel);
        strategy.setColumnNaming(NamingStrategy.underline_to_camel);
        //strategy.setSuperEntityClass("com.baomidou.ant.common.BaseEntity");
        //strategy.setEntityLombokModel(true);
        strategy.setRestControllerStyle(false);
        strategy.setLogicDeleteFieldName("is_delete");
        strategy.setVersionFieldName("version");
        //strategy.setSuperControllerClass("com.baomidou.ant.common.BaseController");
        String[] tableName = {
                "sys_org_user_depart"
        };
        strategy.setInclude(tableName);
        // strategy.setSuperEntityColumns("id");
        strategy.setControllerMappingHyphenStyle(false);
        //strategy.setTablePrefix(pc.getModuleName() + "_");
        mpg.setStrategy(strategy);
        mpg.setTemplateEngine(new FreemarkerTemplateEngine());
        mpg.execute();
    }

}
